layui.define(['layer', 'jquery', 'laytpl'], function (exports) {
  var $ = layui.$,
      layer = layui.layer,
      laytpl = layui.laytpl;

  var pageConf = {
    page: 0,
    size: 10
  }

  var monitor = {

    bindEvent: function () {
      $('.cright').addClass('on');
      //tab 切换事件
      $('.con6-r .nav ul li').click(function () {
        $(this).addClass('on').siblings().removeClass('on');
        $('.tab_container .content').eq(
            $(this).index()).show().siblings().hide();
        var tab_index = $(this).index();
        if (tab_index == 0) {
          monitor.initRecordPagination();
        } else if (tab_index == 1) {
          monitor.initResultPagination();
        }
      })

      $(document).on('click', '.change_status a', function () {
        var $this = $(this),
            id = $(this).data('id');
        $.post('/copyright/monitor/changeStatus', {id: id}, function (result) {
          if (result.success) {
            var data = result.data;
            if ($this.parent().hasClass('zt')) {
              $this.parent().removeClass('zt').addClass('tz');
            } else {
              $this.parent().removeClass('tz').addClass('zt');
            }
            $this.parent().parent().parent().prev().html(data.statusName);
          } else {
            layer.msg('操作失败');
          }
        }, 'json')
      })

      $(document).on('click', '.sc a', function () {
        var $this = $(this),
            id = $this.data('id');

        layer.confirm('确定要删除吗？', {
          btn: ['确定','取消'] //按钮
        }, function(index){
          $.getJSON('/copyright/monitor/delete', {id: id}, function (result) {
            if (result.success) {
              $this.parent().parent().parent().parent().remove();
              layer.close(index);
            }
          })
        }, function(index){

        });

      })

      $(document).on('click', '.hl a', function () {
        var $this = $(this),
            id = $(this).data('id');
        $.getJSON('/copyright/monitor/ingore', {id: id}, function (result) {
          if (result.success) {
            $this.parent().parent().prev().html('已忽略');
          }
        })
      })

    },
    loadRecordData: function () {
      var totalpages = 0;
      $.ajaxSettings.async = false;
      $.getJSON('/copyright/monitor/recordList',pageConf, function (result) {
        if (result.content.length == 0) {
          $('.record_list').hide();
          $('.nothing').show();
          $.get('/account/isVip',function (result) {
            if(!result){
              $('.nothing span').html('您尚未开通该功能');
            }
          })
        } else {
          $('.record_list').show();
          $('.nothing').hide();
          var getTpl = $('#record_body').html(),
              $tbody = $(".record_list tbody");
          laytpl(getTpl).render(result, function (html) {
            $tbody.html(html);
          });
          totalpages = result.totalPages;
        }
      })
      return totalpages;
    },
    initRecordPagination: function () {
      $(".pages").pagination({
        showData: pageConf.size,
        coping: true,
        prevContent: '上一页',
        nextContent: '下一页',
        pageCount:monitor.loadRecordData(),
        callback: function (api) {
          pageConf.page = api.getCurrent() - 1;
          monitor.loadRecordData();
        }
      })
    },
    loadResultData: function () {
      var totalPages = 0;
      $.ajaxSettings.async = false;
      $.getJSON('/copyright/monitor/resultList', pageConf, function (result) {
        if (result.content.length == 0) {
          $('.result_list').hide();
          $('.nothing').show();
          $.get('/account/isVip',function (result) {
            if(!result){
              $('.nothing span').html('您尚未开通该功能');
            }
          })
        } else {
          $('.result_list').show();
          $('.nothing').hide();
          var getTpl = $('#result_body').html(),
              $tbody = $(".result_list tbody");
          laytpl(getTpl).render(result, function (html) {
            $tbody.html(html);
          });
          totalPages = result.totalPages;
          $('.hint-num font').html(result.totalElements);
        }
      })
      return totalPages;
    },
    initResultPagination: function () {
      $(".pages").pagination({
        showData: pageConf.size,
        coping: true,
        prevContent: '上一页',
        nextContent: '下一页',
        pageCount:monitor.loadResultData(),
        callback: function (api) {
          pageConf.page = api.getCurrent() - 1;
          monitor.loadResultData();
        }
      })
    }
  }
  monitor.bindEvent();
  monitor.initRecordPagination();
  exports('monitor', {});
})